package com.guodu.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.guodu.dao.PurchaseBiddingPlantzDao;
import com.guodu.dao.UnitsDao;
import com.guodu.dao.UserInfoDao;
import com.guodu.entity.PurchaseBiddingPlantz;
import com.guodu.entity.PurchasebiddingtzStatus;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.service.PurchaseBiddingPlantzService;
import com.guodu.vo.PurchaseBiddingPlantzQueryVo;
import com.guodu.vo.PurchaseBiddingPlantzVo;

@Service("purchaseBiddingPlantzService")
public class PurchaseBiddingPlantzServiceImpl implements PurchaseBiddingPlantzService {

	@Resource
	private PurchaseBiddingPlantzDao purchaseBiddingPlantzDao;

	@Resource
	private UnitsDao unitsDao;
	
	@Resource
	private UserInfoDao userInfoDao;

	@Override
	public long findSizeByVo(PurchaseBiddingPlantzQueryVo pbpq, Units units) {
		// TODO Auto-generated method stub
		return purchaseBiddingPlantzDao.findSizeByVo(pbpq, units);
	}

	@Override
	public List<PurchaseBiddingPlantzVo> findByVo(
			PurchaseBiddingPlantzQueryVo pbpq, Units units, int curPage,
			int pageSize) {
		// TODO Auto-generated method stub
		List<PurchaseBiddingPlantz> list = purchaseBiddingPlantzDao.findByVo(pbpq,units, curPage, pageSize);
		List<PurchaseBiddingPlantzVo> purchaseBiddingPlantzVos = new ArrayList<PurchaseBiddingPlantzVo>();
		for(PurchaseBiddingPlantz purchaseBiddingPlantz : list ){
			PurchaseBiddingPlantzVo purchaseBiddingPlantzVo = new PurchaseBiddingPlantzVo(purchaseBiddingPlantz);
			UserInfo userInfo = userInfoDao.findById(purchaseBiddingPlantz.getCreatePeople());
			if (userInfo.getUnitsId()!=null) {
				Units unitsInfos = unitsDao.findById(userInfo.getUnitsId());
				purchaseBiddingPlantzVo.setIsComplete(unitsInfos.getIsComplete());
			if (unitsInfos.getIsComplete()!=1&&unitsInfos.getUnitsLevel()==1) {
				Units unitsInfo = unitsDao.findById(purchaseBiddingPlantz.getUnitsId());
				if(unitsInfo!=null){
				purchaseBiddingPlantzVo.setUnitsName(unitsInfo.getUnitsName());
				purchaseBiddingPlantzVo.setIndustry(unitsInfo.getIndustry());

				Units createUnits =  unitsDao.findById(userInfo.getUnitsId());
				purchaseBiddingPlantzVo.setUnitsLevel(createUnits.getUnitsLevel());
				
				if(unitsInfo.getFunitsId()!=null){
					Units funitsInfo = unitsDao.findById(unitsInfo.getFunitsId());
					purchaseBiddingPlantzVo.setFunitsName(funitsInfo.getUnitsName());
				}
				}
			}else if(unitsInfos.getIsComplete()!=1&&unitsInfos.getUnitsLevel()==2){
				
					Units unitsInfo = unitsDao.findById(purchaseBiddingPlantz.getUnitsId());
					Units funits = unitsDao.findById(unitsInfo.getFunitsId());
					purchaseBiddingPlantzVo.setUnitsName(unitsInfo.getUnitsName());
					purchaseBiddingPlantzVo.setFunitsName(funits.getUnitsName());
					purchaseBiddingPlantzVo.setIndustry(unitsInfo.getIndustry());
					Units createUnits =  unitsDao.findById(userInfo.getUnitsId());
					purchaseBiddingPlantzVo.setUnitsLevel(createUnits.getUnitsLevel());
					
			}else if(unitsInfos.getIsComplete()==1&&purchaseBiddingPlantz.getUnitsId()==null){
				purchaseBiddingPlantzVo.setUnitsLevel(unitsInfos.getUnitsLevel());
			}else if(unitsInfos.getIsComplete()==1&&purchaseBiddingPlantz.getUnitsId()!=null){
				Units unitsInfo = unitsDao.findById(purchaseBiddingPlantz.getUnitsId());
				purchaseBiddingPlantzVo.setUnitsLevel((long)1);
				purchaseBiddingPlantzVo.setIndustry(unitsInfo.getIndustry());
				if (unitsInfo.getFunitsId()!=null) {
					Units funits = unitsDao.findById(unitsInfo.getFunitsId());
					purchaseBiddingPlantzVo.setUnitsName(unitsInfo.getUnitsName());
					purchaseBiddingPlantzVo.setFunitsName(funits.getUnitsName());
				}else{
					Units funits = unitsDao.findById(unitsInfo.getUnitsId());
					purchaseBiddingPlantzVo.setFunitsName(funits.getUnitsName());
				}
			}
			}else{
				Long id = null;
				purchaseBiddingPlantzVo.setUnitsLevel(id);
				if (purchaseBiddingPlantz.getUnitsId()!=null&&!"".equals(purchaseBiddingPlantz.getUnitsId())) {
					Units unitsInfo = unitsDao.findById(purchaseBiddingPlantz.getUnitsId());
					purchaseBiddingPlantzVo.setIndustry(unitsInfo.getIndustry());
					if (unitsInfo.getFunitsId()!=null) {
						Units funits = unitsDao.findById(unitsInfo.getFunitsId());
						purchaseBiddingPlantzVo.setUnitsName(unitsInfo.getUnitsName());
						purchaseBiddingPlantzVo.setFunitsName(funits.getUnitsName());
					}else{
						Units funits = unitsDao.findById(unitsInfo.getUnitsId());
						purchaseBiddingPlantzVo.setFunitsName(funits.getUnitsName());
					}
				}
			}
			purchaseBiddingPlantzVo.setUserName(userInfo.getUserName());
			purchaseBiddingPlantzVos.add(purchaseBiddingPlantzVo);
		}
		return purchaseBiddingPlantzVos;
	}

	@Override
	public void save(PurchaseBiddingPlantz purchaseBiddingPlantz) {
		// TODO Auto-generated method stub
		purchaseBiddingPlantzDao.save(purchaseBiddingPlantz);
	}

	@Override
	public PurchaseBiddingPlantz findById(String purchaseId) {
		// TODO Auto-generated method stub
		return purchaseBiddingPlantzDao.findById(purchaseId);
	}

	@Override
	public void update(PurchaseBiddingPlantz purchaseBiddingPlantz,Long status) {
		// TODO Auto-generated method stub
		purchaseBiddingPlantzDao.update(purchaseBiddingPlantz);
		List<PurchasebiddingtzStatus> entities = new ArrayList<PurchasebiddingtzStatus>();
		PurchasebiddingtzStatus entity = new PurchasebiddingtzStatus();
		entity.setAfterStatus(Long.valueOf(0));
		entity.setBeforeStatus(status);
		entity.setCreatePeople(purchaseBiddingPlantz.getCreatePeople());
		entity.setPurchaseId(purchaseBiddingPlantz.getPurchaseId());
		entity.setStatusId("1");
		entity.setCreateTime(Calendar.getInstance().getTime());
		entities.add(entity);
		purchaseBiddingPlantzDao.changeStatus(entities);
	}

	@Override
	public List<PurchasebiddingtzStatus> findStatus(String purchaseId) {
		// TODO Auto-generated method stub
		return purchaseBiddingPlantzDao.findStatus(purchaseId);
	}

	@Override
	public void examine(String ids, String rs, String beforeStatus,
			Long status, UserInfo user) {
		// TODO Auto-generated method stub
		String[] idses = ids.split(",");
		String[] beforeStatuses = beforeStatus.split(",");
		List<PurchasebiddingtzStatus> entities = new ArrayList<PurchasebiddingtzStatus>();
		for(int i=0;i<idses.length;i++){
			PurchasebiddingtzStatus entity = new PurchasebiddingtzStatus();
			entity.setAfterStatus(status);
			entity.setBeforeStatus(Long.valueOf(beforeStatuses[i]));
			entity.setCreatePeople(user.getUserId());
			entity.setPurchaseId(idses[i]);
			entity.setReason(rs);
			entity.setStatusId(i+"");
			entity.setCreateTime(Calendar.getInstance().getTime());
			entities.add(entity);
		}
		
		purchaseBiddingPlantzDao.examine(ids,status);
		purchaseBiddingPlantzDao.changeStatus(entities);
	}

	@Override
	public void delByIds(String[] delIds) {
		// TODO Auto-generated method stub
		purchaseBiddingPlantzDao.delByIds(delIds);
	}

	@Override
	public List<PurchaseBiddingPlantz> findAllByVo(
			PurchaseBiddingPlantzQueryVo pbpq, Units attribute) {
		// TODO Auto-generated method stub
		return purchaseBiddingPlantzDao.findAllByVo(pbpq,attribute);
	}

	
	public PurchaseBiddingPlantzVo findAllMoney(
			PurchaseBiddingPlantzQueryVo pbpq, Units units) {
		
		return purchaseBiddingPlantzDao.findAllMoney(pbpq,units);
	}

	@Override
	public void examinAll(PurchaseBiddingPlantzQueryVo pbpq, Units units) {
		// TODO Auto-generated method stub
		purchaseBiddingPlantzDao.examinAll(pbpq, units);
	}
	

	

	
	

}
